package com.example.materialviewpagerdemo;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v4.app.NotificationCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.materialviewpagerdemo.Presenter.CurrentDataController;
import com.example.materialviewpagerdemo.Presenter.CurrentDataController4;
import com.example.materialviewpagerdemo.bean.Dezhou;
import com.example.materialviewpagerdemo.bean.ListViewModel;
import com.example.materialviewpagerdemo.bean.Shouguang;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.util.SPHelper;
import com.example.materialviewpagerdemo.view.OnItemClickListener;
import com.example.materialviewpagerdemo.view.Time;
import com.github.florent37.materialviewpager.header.MaterialViewPagerHeaderDecorator;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RecyclerViewFragment extends Fragment {
    private static final String TAG = "RecyclerViewFragment";

    public static Fragment newInstance() {
        return new RecyclerViewFragment();
    }

    final List<ListViewModel> items = new ArrayList<>();
    static final int ITEMS = 11;
    ListViewModel[] listViewModels = new ListViewModel[ITEMS];

    private ZLoadingDialog dialog;
    RecyclerView mRecyclerView;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return inflater.inflate(R.layout.fragment_recyclerview, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(R.id.recyclerView);
        dialog = new ZLoadingDialog(RecyclerViewFragment.this.getActivity());

        for (int i = 0; i < listViewModels.length; i++) {
            listViewModels[i] = new ListViewModel();
        }

        listViewModels[0].setImgId(R.drawable.ic_time);
        listViewModels[0].setDataName("获取时间");
        //
        listViewModels[1].setImgId(R.drawable.ic_airtem);
        listViewModels[1].setDataName("空气温度1");
        //
        listViewModels[2].setImgId(R.drawable.ic_airhum);
        listViewModels[2].setDataName("空气湿度1");

        listViewModels[3].setImgId(R.drawable.ic_airtem);
        listViewModels[3].setDataName("空气温度2");
        //
        listViewModels[4].setImgId(R.drawable.ic_airhum);
        listViewModels[4].setDataName("空气湿度2");
        listViewModels[5].setImgId(R.drawable.ic_airtem);
        listViewModels[5].setDataName("CO2");
        listViewModels[6].setImgId(R.drawable.ic_sunshine);
        listViewModels[6].setDataName("阳光1");
        listViewModels[7].setImgId(R.drawable.ic_sunshine);
        listViewModels[7].setDataName("阳光2");

        listViewModels[8].setImgId(R.drawable.ic_airhum);
        listViewModels[8].setDataName("土壤湿度");

        listViewModels[9].setImgId(R.drawable.ic_airtem);
        listViewModels[9].setDataName("土壤电导率");
        //
        listViewModels[10].setImgId(R.drawable.ic_airtem);
        listViewModels[10].setDataName("土壤温度");

        //

        //

        //
        for (ListViewModel listViewModel : listViewModels) {
            items.add(listViewModel);
        }

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new MaterialViewPagerHeaderDecorator());
        RecyclerViewPagerAdapter recyclerViewPagerAdapter = new RecyclerViewPagerAdapter(this.getActivity(), items);
        recyclerViewPagerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {

                if (postion == 0) {
                    Intent intent = new Intent(RecyclerViewFragment.this.getActivity(), Time.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(RecyclerViewFragment.this.getActivity(), ChartActivity.class);
                    intent.putExtra("id", postion);
                    startActivity(intent);
                }
            }
        });
        mRecyclerView.setAdapter(recyclerViewPagerAdapter);
        dialog.setLoadingBuilder(Z_TYPE.STAR_LOADING)//设置类型
                .setLoadingColor(getResources().getColor(R.color.accent_color))//颜色
                .setHintText("正在加载...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(getResources().getColor(R.color.accent_color))  // 设置字体颜色
                .setDurationTime(1) // 设置动画时间百分比 - 0.5倍
                .setDialogBackgroundColor(getResources().getColor(R.color.colorWhite)) // 设置背景色，默认白色
                .show();

        getCurrentData("1");

    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
        public void getCurrentData(String flag) {
        SPHelper spHelper = new SPHelper(getActivity(), "URL");
        Retrofit retrofit = new Retrofit.Builder().baseUrl(StaticData.url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CurrentDataController4 currentDataController = retrofit.create(CurrentDataController4.class);
        Call<Shouguang> currentDataCall = currentDataController.queryOne("01");
        Log.i("LLL","LLL"+flag);
        currentDataCall.enqueue(new Callback<Shouguang>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onResponse(Call<Shouguang> call, Response<Shouguang> response) {
                System.out.println("========="+response.body());
              //  System.out.println("onresucss"+response.body().getDatetime());

                try {
                    listViewModels[0].setDataContent(response.body().getDatetime().substring(2, 16));
                    listViewModels[1].setDataContent(response.body().getAirtemp1() + "℃");
                    listViewModels[2].setDataContent(response.body().getAirhumi1() + "%");
                    listViewModels[3].setDataContent(response.body().getAirtemp2() + "℃");
                    listViewModels[4].setDataContent(response.body().getAirhumi2() + "%");
                    listViewModels[5].setDataContent(response.body().getCoo() + "ppm");
                    listViewModels[6].setDataContent(response.body().getSunlight1() + "kLux");
                    listViewModels[7].setDataContent(response.body().getSunlight2() + "kLux");
                    listViewModels[8].setDataContent(response.body().getSoilhumi() + "%");
                    listViewModels[9].setDataContent(response.body().getSoilelc() + "µs/m");
                    listViewModels[10].setDataContent(response.body().getSoiltemp() + "℃");


                } catch (NullPointerException e) {
                    for (int i = 0; i < listViewModels.length; i++) {
                        listViewModels[i].setDataContent("");
                    }
                    Toast.makeText(RecyclerViewFragment.this.getActivity(), "数据获取失败,请检查网络后重试", Toast.LENGTH_SHORT).show();
                }
                RecyclerViewPagerAdapter recyclerViewPagerAdapter = new RecyclerViewPagerAdapter(RecyclerViewFragment.this.getActivity(), items);
                recyclerViewPagerAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int postion) {
                        if (postion == 0) {
                            Intent intent = new Intent(RecyclerViewFragment.this.getActivity(), Time.class);
                            intent.putExtra("datatime", response.body().getDatetime().substring(0, 19));
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(RecyclerViewFragment.this.getActivity(), ChartActivity.class);
                            intent.putExtra("id", postion);
                            intent.putExtra("flag", flag);
                            startActivity(intent);
                        }
                    }
                });
                mRecyclerView.setAdapter(recyclerViewPagerAdapter);
                dialog.cancel();
            }

            @Override
            public void onFailure(Call<Shouguang> call, Throwable t) {
                t.printStackTrace();
                dialog.cancel();
                System.out.println("连接超时");
            }
        });


    }

}
